﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace AdHoc.Wavesque.Test
{
    [TestFixture(Description = "Unit tests for class SawThoothesque.")]
    public class SawToothesqueTest
    {
        [Test(Description = "Create an object representation of a sawtooth wave.")]
        public void Create()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Step, SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(SawToothesque), f);
        }

        [Test(Description = "Create an object representation of a sawtooth wave.")]
        public void Create2()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);
            Assert.IsInstanceOf(typeof(SawToothesque), f);
        }

        [Test(Description = "Creates a synchronized object representation of a sawtooth wave.")]
        public void Create3()
        {
            var f = SawToothesque.Synchronized(new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase));
            Assert.IsInstanceOf(typeof(SawToothesque), f);
            Assert.IsTrue(f.IsSynchronized);
        }

        [Test(Description = "Generate a default number of points of a sawtooth wave.")]
        public void GetValuesTest()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sawtooth wave.")]
        public void GetValuesTest2()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave.")]
        public void GetValuesTest3()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(SawToothesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave.")]
        public void GetValuesTest4()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.GetValues(SawToothesqueTestCaseData.NumberOfPoints, out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave.")]
        public void GetValuesTest5()
        {
            var f = SawToothesque.Synchronized(new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase));
            Assert.IsTrue(f.IsSynchronized);

            double[] x;
            double[] y;
            f.GetValues(SawToothesqueTestCaseData.NumberOfPoints, out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }   
        
        [Test(Description = "Generate a given number of points of a sawtooth wave."), TestCaseSource(typeof(SawToothesqueTestCaseData), "Data")]
        public void GetValuesTest(double step, double amplitude, double frequency, double phase, double x0, uint numberOfPoints)
        {
            double[] aX;
            double[] aY;

            var f = new SawToothesque(step, amplitude, frequency, phase);
            f.GetValues(numberOfPoints, out aX, out aY, x0);
            
            Assert.AreEqual(numberOfPoints, aY.Length);
            
            List<double> bX = new List<double>();
            List<double> bY = new List<double>();
                        
            foreach(double pos in aX)
            {
            	double[] x;
            	double[] y;
            	var g = new SawToothesque(step, amplitude, frequency, phase);
            	g.GetValues(1, out x, out y, pos);
            	
            	Assert.AreEqual(1, x.Length);
            	Assert.AreEqual(1, y.Length);
            	
            	bX.AddRange(x);
            	bY.AddRange(y);
            }
            
            Assert.AreEqual(aX, bX);
            Assert.AreEqual(aY, bY);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave, half wave rectification.")]
        public void ToHalfWaveRectification()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SawToothesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave, half wave rectification.")]
        public void ToHalfWaveRectification2()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(SawToothesqueTestCaseData.NumberOfPoints, out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sawtooth wave, half wave rectification.")]
        public void ToHalfWaveRectification3()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sawtooth wave, half wave rectification.")]
        public void ToHalfWaveRectification4()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToHalfWaveRectification(out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave, full wave rectification.")]
        public void ToFullWaveRectification()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SawToothesqueTestCaseData.NumberOfPoints, out x, out y);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave, full wave rectification.")]
        public void ToFullWaveRectification2()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(SawToothesqueTestCaseData.NumberOfPoints, out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(SawToothesqueTestCaseData.NumberOfPoints, y.Length);
        }

        [Test(Description = "Generate a given number of points of a sawtooth wave, full wave rectification.")]
        public void ToFullWaveRectification3()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y);

            Assert.AreEqual(100, y.Length);
        }

        [Test(Description = "Generate a default number of points of a sawtooth wave, full wave rectification.")]
        public void ToFullWaveRectification4()
        {
            var f = new SawToothesque(SawToothesqueTestCaseData.Amplitude, SawToothesqueTestCaseData.Frequency, SawToothesqueTestCaseData.Phase);

            double[] x;
            double[] y;
            f.ToFullWaveRectification(out x, out y, SawToothesqueTestCaseData.X0);

            Assert.AreEqual(100, y.Length);
        }
    }
}
